﻿using ConfigInfoWebPlatform.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConfigInfoWebPlatform.API.DBCommon
{
    /// <summary>
    /// 角色数据层
    /// </summary>
    public class DAL_Role
    {
        private IFreeSql freeSql;

        /// <summary>
        /// 角色数据层
        /// </summary>
        public DAL_Role()
        {
            freeSql = FreesqlFactory.GetFreeSql("configplatform");
        }

        /// <summary>
        /// 查询角色信息
        /// </summary>
        /// <returns></returns>
        public List<RoleInfoModel> GetRoleList(string name, string did, bool? isAdmin)
        {
            //查询角色信息
            var query = freeSql.Select<Role, Department>()
                             .InnerJoin((a, b) => a.DId == b.Id)
                             .WhereIf(!string.IsNullOrWhiteSpace(name), (a, b) => a.Name.Contains(name))
                             .WhereIf(isAdmin == false, (a, b) => a.DId == did.Replace("-", ""))
                             .ToList((a, b) => new RoleInfoModel()
                             {
                                 Id = a.Id,
                                 Name = a.Name,
                                 Code = a.Code,
                                 Did = a.DId,
                                 DName = b.Name
                             });

            return query;
        }
             
        /// <summary>
        /// 根据角色Id获取对应的菜单Id
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        public List<long> GetProgramNavicatIds(string rid)
        {
            var query = freeSql.Select<Authority, ProgramNavicat>()
                            .InnerJoin((a, b) => a.PNId == b.Id.ToString())
                            .Where((a, b) => b.Level != 1 && a.RId == rid)
                            .ToList((a, b) => b.Id);

            return query;
        }

        /// <summary>
        /// 根据角色Id获取对应的菜单parentId
        /// </summary>
        public List<long> GetprogramNavicatPids(string rid)
        {
            var query= freeSql.Select<Authority, ProgramNavicat>()
                            .InnerJoin((a, b) => a.PNId == b.Id.ToString())
                            .Where((a, b) => b.Level != 1 && a.RId == rid)
                            .ToList((a, b) =>Convert.ToInt64(b.ParentId));
            return query.Distinct().ToList();
        }


        /// <summary>
        /// 添加角色权限
        /// </summary>
        public int InsertRoleAuthority(Authority model)
        {
            var result = freeSql.Insert<Authority>(model).ExecuteAffrows();
            return result;
        }


        /// <summary>
        /// 根据角色Id删除权限
        /// </summary>
        public int DeleteRoleAuthority(string rid)
        {
            var result = freeSql.Delete<Authority>().Where(x => x.RId == rid).ExecuteAffrows();
            return result;
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        public int DeleteRole(string rid)
        {
            var result = freeSql.Delete<Role>().Where(x => x.Id == rid).ExecuteAffrows();
            return result;
        }

        /// <summary>
        /// 角色详情数据
        /// </summary>
        public RoleModel GetRoleDetail(string rid)
        {
            
            var query = freeSql.Select<Role>()
                             .Where(x => x.Id == rid)
                             .ToOne(x=>new RoleModel { 
                                Id=x.Id,
                                Code=x.Code,
                                Did=x.DId,
                                Name=x.Name,
                                OperateAuthority=x.OperateAuthority
                             });
            return query;
        }

        /// <summary>
        /// 添加角色信息
        /// </summary>
        public int InsertRole(Role model)
        {
            var result = freeSql.Insert<Role>(model).ExecuteAffrows();
            return result;
        }

        /// <summary>
        /// 编辑角色信息
        /// </summary>
        public int EditRole(Role model)
        {
            var result = freeSql.Update<Role>()
                              .SetSource(model)
                              .ExecuteAffrows();
            return result;
        }

        /// <summary>
        /// 获取部门数据
        /// </summary>
        /// <returns></returns>
        public List<DepartmentModel> GetDepartments()
        {
            var query = freeSql.Select<Department>()
                             .ToList(x => new DepartmentModel
                             {
                                 Id = x.Id,
                                 Name = x.Name
                             });
            
            return query;
        }

        /// <summary>
        /// 根据部门Id角色Code查看角色是否存在
        /// </summary>
        /// <returns></returns>
        public bool IsExists(string id="", string did="", string code="",bool isRepeat=true)
        {
            var query = freeSql.Select<Role>()
                             .WhereIf(!string.IsNullOrWhiteSpace(id) && isRepeat, x => x.Id != id)
                             .WhereIf(!string.IsNullOrWhiteSpace(id) && !isRepeat, x => x.Id == id)
                             .WhereIf(!string.IsNullOrWhiteSpace(did), x => x.DId == did)
                             .WhereIf(!string.IsNullOrWhiteSpace(code), x => x.Code == code)
                             .Count();
            return query > 0 ? true : false;
        }

        /// <summary>
        /// 通过角色Id获取角色操作权限 
        /// </summary>
        /// <returns></returns>
        public int? GetOperateAuthorityByRid(string rid)
        {
            int? result = 0;
            if(!string.IsNullOrWhiteSpace(rid))
            {
                result = freeSql.Select<Role>()
                                .Where(x => x.Id == rid)
                                .ToOne(x => x.OperateAuthority);
            }
            return result;
        }
    }
}
